package com.zzvcom.template.action.mapper;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.dispatcher.mapper.DefaultActionMapper;

import com.opensymphony.xwork2.config.ConfigurationManager;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

public class Restful2ActionMapper extends DefaultActionMapper
{

    protected static final Logger LOG = LoggerFactory
            .getLogger(Restful2ActionMapper.class);

    public Restful2ActionMapper()
    {
        setSlashesInActionNames("true");
    }

    public ActionMapping getMapping(HttpServletRequest request,
            ConfigurationManager configManager)
    {
        if (!isSlashesInActionNames())
        {
            throw new IllegalStateException(
                    "This action mapper requires the setting 'slashesInActionNames' to be set to 'true'");
        }
        ActionMapping mapping = super.getMapping(request, configManager);

        if (mapping == null)
        {
            return null;
        }

        String actionName = mapping.getName();
        System.out.println("action name:" + actionName);
        // Only try something if the action name is specified
        if (actionName != null && actionName.length() > 0)
        {

            int lastSlashPos = actionName.lastIndexOf('/');
            if (lastSlashPos < 0)
            {
                // 没有设置 /*/* 格式 访问默认的方法
                return mapping;
            }

            // If a method hasn't been explicitly named, try to guess using
            // ReST-style patterns
            if (mapping.getMethod() == null && lastSlashPos > -1)
            {
                // 最后一个参数为方法名称
                mapping.setMethod(actionName.substring(lastSlashPos + 1));
            }

        }

        return mapping;
    }

}
